package com.demo.core.dao.impl;

import java.util.List;
import java.util.Map;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.AreaDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Area;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * 区域数据访问实现类
 * 
 * @author ryuu.kk
 *
 */
public class AreaDaoImpl extends PageCoreDaoSupport<Area, Long> implements AreaDao {

	/**
	 * 配置空间 
	 */
	protected final static String AREA_SPACE = "areaSpace.";
	
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_AREA";
	
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Area> tree(Map<String, Object> params) {
		
		List<Area> result = null;
		CacheKey key = null;
		int hashCode = params.hashCode();
		CacheWrapper<CacheKey, Object> cache = null;
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			key.update("search");
			key.update(hashCode);
			result = (List<Area>) cache.get(key);
		}
		if (result != null) {
			return result;
		}
		
		result = getSqlMapClientTemplate().queryForList(model.getNameSpace() + "queryTree", params);
		
		if (result != null && isUseCache() && cache != null) {
			key = new CacheKey();
			key.update("tree");
			key.update(hashCode);
			cache.put(key, result);
		}
		return result;
	}
	
	@Override
	protected String nameSpace() {
		return AREA_SPACE;
	}

	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setTable(TABLE_NAME);
		model.setPk(TABLE_PK_NAME);
		model.setNameSpace(AREA_SPACE);
	}
}
